package com.hanrui.android.net.presenter.message.myvote;

import android.content.Context;
import android.util.Log;

import com.hanrui.android.activity.LoginActivity;
import com.hanrui.android.library.base.BaseBean;
import com.hanrui.android.library.base.BasePresenter;
import com.hanrui.android.constant.Constant;
import com.hanrui.android.model.MyVoteProposalBean;
import com.hanrui.android.library.net.ApiService;
import com.hanrui.android.library.net.RetrofitManager;
import com.hanrui.android.library.net.RxSchedulers;
import com.hanrui.android.net.contract.message.myvote.MyVoteHandleContract;

import javax.inject.Inject;

import io.reactivex.functions.Consumer;

/**
 *
 */

public class MyVoteHandlePresenter extends BasePresenter<MyVoteHandleContract.View> implements MyVoteHandleContract.Presenter {

    @Inject
    public MyVoteHandlePresenter(){}

    @Override
    public void proposalDetail(final Context context,String mid,String type) {
        mView.showLoading();
        RetrofitManager.create(ApiService.class, Constant.baseUrl)
                .proposalDetail(mid,type)
                .compose(RxSchedulers.<BaseBean<MyVoteProposalBean>>applySchedulers())
                .compose(mView.<BaseBean<MyVoteProposalBean>>bindToLife())
                .subscribe(new Consumer<BaseBean<MyVoteProposalBean>>() {
                    @Override
                    public void accept(BaseBean<MyVoteProposalBean> dataResponse) throws Exception {
                        Log.e(Constant.tag,dataResponse.getCode()+"");
                        mView.hideLoading();
                        if (dataResponse.getCode() == 1){
                            mView.proposalDetail(dataResponse.getData());
                        }else if (dataResponse.getCode() == 201){
                            LoginActivity.start(context);
                            mView.showFaild("会话过期请重新登录");
                        }else {
                            mView.showFaild(String.valueOf(dataResponse.getMessage()));
                        }

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.hideLoading();
                        mView.showFaild(throwable.getMessage());
                    }
                });

    }

    @Override
    public void saveProposalVote(final Context context, String rid, String proposalList) {
        mView.showLoading();
        RetrofitManager.create(ApiService.class, Constant.baseUrl)
                .saveProposalVote(rid,proposalList)
                .compose(RxSchedulers.<BaseBean>applySchedulers())
                .compose(mView.<BaseBean>bindToLife())
                .subscribe(new Consumer<BaseBean>() {
                    @Override
                    public void accept(BaseBean dataResponse) throws Exception {
                        Log.e(Constant.tag,dataResponse.getCode()+"");
                        mView.hideLoading();
                        if (dataResponse.getCode() == 201){
                            LoginActivity.start(context);
                            mView.showFaild("会话过期请重新登录");
                        }else {
                            mView.saveProposalVote(dataResponse.getCode());
                        }

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.hideLoading();
                        mView.showFaild(throwable.getMessage());
                    }
                });
    }
}
